home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / editor / auror300.zip / SWEEP.AML < prev    next >
Text File  |  1996-07-17  |  4KB  |  124 lines

  1. //--------------------------------------------------------------------
  2. // SWEEP.AML
  3. // Sweep Directories, (C) 1993-1996 by nuText Systems
  4. //
  5. // This macro will traverse a directory and all it's subdirectories,
  6. // performing one or more of the following operations:
  7. //
  8. //   - copying files and directories
  9. //   - deleting files and directories
  10. //   - obtaining statistics
  11. //
  12. // Usage:
  13. //
  14. // This macro is intended for use as a utility by other macros, not as a
  15. // standalone macro. Sweep is used by the file manager to mopy, copy, and
  16. // delete entire directories, and to obtain directory statistics.
  17. //
  18. // When called from runmacro, arguments to this macro will be as follows:
  19. //
  20. //   arg 1: the filename of this macro
  21. //   arg 2: the object name of this macro
  22. //   arg 3: the source directory
  23. //   arg 4: the destination directory
  24. //   arg 5: option codes
  25. //
  26. // All directories must have a terminating backslash. The following
  27. // table shows what arguments must be passed (from runmacro) for each
  28. // sweep function:
  29. //
  30. //   function      arg 3      arg 4      arg 5
  31. //   --------      -----      -----      -----
  32. //   move          source\    dest\      'cd'
  33. //   copy          source\    dest\      'c'
  34. //   delete        source\    null       'd'
  35. //   statistics    source\    null       null
  36. //
  37. // This macro will also return a 3-element array consisting of:
  38. //
  39. //   element 1:    the total number of directories
  40. //   element 2:    the total number of files
  41. //   element 3:    the total size of all files
  42. //
  43. // If anything fails, this macro returns null.
  44. //--------------------------------------------------------------------
  45.  
  46. // compile time macros and function definitions
  47. include bootpath "define.aml"
  48.  
  49. variable totaldirs, totalfiles, totalsize
  50.  
  51. loadoptions = "dfhvs" + _NameStyle
  52.  
  53. // scan directories recursively
  54. // directories must have a terminating backslash
  55. private function sweep (spath dpath options)
  56.   // get a directory listing
  57.   if loadbuf spath + "*.*" '' '' loadoptions then
  58.     totaldirs = totaldirs + (getloadinfo 'd')
  59.     totalfiles = totalfiles + (getloadinfo 'f')
  60.     totalsize = totalsize + (getloadinfo 's')
  61.     c = pos 'c' options
  62.     d = pos 'd' options
  63.     if c then
  64.       if not (createdir dpath) and not (directory? dpath) then
  65.         destroybuf
  66.         return
  67.       end
  68.     end
  69.     if getlinelen then
  70.       repeat
  71.         file = gettext
  72.         sfile = spath + file
  73.         if file [LAST_CHAR] == '\\' then
  74.           if not sweep sfile dpath + file options then
  75.             destroybuf
  76.             return
  77.           end
  78.         else
  79.           if c then
  80.             if not copyfile sfile dpath + file then
  81.               destroybuf
  82.               return
  83.             end
  84.           end
  85.           if d then
  86.             // delete the file even if it's read-only
  87.             if not deletefile sfile then
  88.               setfileattr sfile
  89.               if not deletefile sfile then
  90.                 destroybuf
  91.                 return
  92.               end
  93.             end
  94.           end
  95.         end
  96.       until not down
  97.     end
  98.  
  99.     // delete the source directory
  100.     if d then
  101.       // delete the file even if it's read-only
  102.       if not deletefile spath then
  103.         spath [LAST_CHAR] = ''
  104.         setfileattr spath
  105.         if not deletefile spath then
  106.           destroybuf
  107.           return
  108.         end
  109.       end
  110.     end
  111.  
  112.     destroybuf
  113.   end
  114.   return TRUE
  115. end
  116.  
  117. // move:   source\ dest\ 'cd'
  118. // copy:   source\ dest\ 'c'
  119. // delete: source\ ''    'd'
  120. // stats:  source\ ''    ''
  121. if sweep (arg 3) (arg 4) (arg 5) then
  122.   return {totaldirs totalfiles totalsize}
  123. end
  124.